package com.ldzl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ldzl.pojo.CkProductSalesLine;
import com.ldzl.pojo.CkStockDetail;
import com.ldzl.pojo.CkStorageArea;
import com.ldzl.service.CkProductSalesLineService;
import com.ldzl.mapper.CkProductSalesLineMapper;
import com.ldzl.service.CkStockDetailService;
import com.ldzl.service.CkStorageAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 风止
* @description 针对表【ck_product_sales_line(产品销售出库行表)】的数据库操作Service实现
* @createDate 2025-07-24 17:24:37
*/
@Service
@Transactional
public class CkProductSalesLineServiceImpl extends ServiceImpl<CkProductSalesLineMapper, CkProductSalesLine>
    implements CkProductSalesLineService{

    @Autowired
    private CkStockDetailService sds; // 库存详情
    @Autowired
    private CkStorageAreaService sas; // 仓库 库位

    /**
     * 查询指定销售出库单的商品详情
     * @param sales_id
     * @return
     */
    @Override
    public List<CkProductSalesLine> selectCkProductSalesLine(Long sales_id) {
        return super.list(new QueryWrapper<CkProductSalesLine>()
                .eq("sales_id",sales_id)
                .eq("is_delete",0)
        );
    }

    /**
     * 出库
     * @param sales_id
     * @return
     */
    @Override
    public boolean warehouseOutbound(Long sales_id,String update_by) {
        Date date = new Date();

        List<CkProductSalesLine> lines = selectCkProductSalesLine(sales_id);
        for (CkProductSalesLine line : lines) {
            if(line.getStatus().equals("2")){ //出库合格的产品
                System.out.println("测试出库的数量："+line.getSales_num());
                System.out.println("测试出库的产品编号："+line.getProduct_code());

                //查询库存信息
                List<CkStockDetail> stockList = sds.list(new QueryWrapper<CkStockDetail>()
                        .eq("item_code",line.getProduct_code())
                        .eq("batch_code",line.getBatch_code())
                        .eq("is_delete",0)
                );

                CkStockDetail stock = new CkStockDetail(); //要修改的库存
                stock.setItem_id(stockList.get(0).getItem_id());
                stock.setQuantity_num(stockList.get(0).getQuantity_num().subtract(line.getSales_num()));
                stock.setUpdate_by(update_by);
                stock.setUpdate_time(date);
                if(!sds.saveOrUpdate(stock))
                    return false;
                //查询出库位信息
                CkStorageArea area = sas.getOne(new QueryWrapper<CkStorageArea>()
                        .eq("area_id",stockList.get(0).getArea_id())
                        .eq("is_delete",0)
                );

                //减少库位的 已用容量
                CkStorageArea area2 = new CkStorageArea();
                area.setArea_id(stockList.get(0).getArea_id());
                //单位转换 暂不处理
                area.setCurrent_weight(area.getCurrent_weight().subtract(line.getSales_num()));
                area.setUpdate_by(update_by);
                area.setUpdate_time(date);
                if(!sas.saveOrUpdate(area))
                    return false; //扣减库位已用容量失败
            }
        }
        return true;
    }


}




